package contreller

import (
	"SkillTextJD2/Util"
	"SkillTextJD2/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"golang.org/x/crypto/bcrypt"
	"log"
	"net/http"
)

func HandlerIndex(c *gin.Context) {
	var Attractions []model.Attractions
	Util.DB().Table("attractions").Find(&Attractions)
	c.HTML(http.StatusOK, "index.html", gin.H{"code": 200, "res": Attractions})
	//c.HTML(http.StatusOK, "index.html", "200")
}

func HandlerInformation(c *gin.Context) {
	var Informations []model.Information
	Util.DB().Table("information").Find(&Informations)
	c.HTML(http.StatusOK, "information.html", gin.H{"code": 200, "out": Informations})
}

func HandlerBuy(c *gin.Context) {
	var But []model.But
	Util.DB().Table("payment").Find(&But)
	c.HTML(http.StatusOK, "buy.html", gin.H{"code": 200, "ccd": But})
	//c.HTML(http.StatusOK, "buy.html", "200")
}

func HandlerScenery(c *gin.Context) {
	c.HTML(http.StatusOK, "scenery.html", "200")

}

// 查询城市---模糊查询
func HandlerInquire(c *gin.Context) {
	cityName := c.DefaultQuery("cityName", "")
	if cityName == "" {
		cityName = c.PostForm("cityName")
	}

	var attraction []model.Attractions
	err := Util.DB().
		Table("attractions").
		Where("attractions_name LIKE ?", "%"+cityName+"%").
		Find(&attraction).Error

	if err != nil {
		log.Println("查询出错：", err)
	}

	log.Println("查询参数：", cityName)
	log.Println("查询结果数量：", len(attraction))

	c.HTML(http.StatusOK, "index.html", gin.H{
		"res": attraction,
	})
}

// 去注册
func HandlerToRegister(c *gin.Context) {
	c.HTML(http.StatusOK, "toregister.html", "200")
}

// 注册
func HandlerRegister(c *gin.Context) {
	c.HTML(http.StatusOK, "register.html", "200")
	u := model.User{}
	fmt.Println(u)
	if u.Username == "" || u.Password == "" {
		c.JSON(http.StatusBadRequest, gin.H{"msg": "用户名或密码不能为空"})
		return
	}
	res := Util.DB().Where("username = ?", u.Username).First(&model.User{})
	if res.RowsAffected != 0 {
		c.JSON(http.StatusOK, gin.H{"msg": "用户名已存在"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "注册成功"})
	http.Redirect(c.Writer, c.Request, "/index", http.StatusFound)

}

func HandlerToLogin(c *gin.Context) {
	c.HTML(http.StatusOK, "login.html", nil)
}

// 登录
func HandlerLogin(c *gin.Context) {
	//请求获取用户名和密码
	name := c.Query("username")
	password := c.Query("password")
	fmt.Println(name, password)
	if name == "" || password == "" {
		c.JSON(http.StatusBadRequest, gin.H{"msg": "用户名或密码不能为空"})
		return
	}
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"msg": "服务器内部错误"})
		return
	}
	res := Util.DB().Where("username = ?", name).First(&model.User{})
	if res.RowsAffected != 1 {
		c.JSON(http.StatusOK, gin.H{"msg": "用户名或密码错误"})
		return
	}
	//对密码进行哈希处理
	err = bcrypt.CompareHashAndPassword(hashedPassword, []byte(password))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "用户名或密码错误"})
		return
	}
	//找到一条记录相匹配就登录成功
	//c.JSON(http.StatusOK, gin.H{"msg": "登录成功"})
	http.Redirect(c.Writer, c.Request, "/index", http.StatusFound)
}

func HandlerChangsha(c *gin.Context) {
	c.HTML(http.StatusOK, "changsha.html", "200")
}

func HandlerShanghai(c *gin.Context) {
	c.HTML(http.StatusOK, "shanghai.html", "200")
}

func HandlerAttraction(c *gin.Context) {
	cityName := c.PostForm("cityName")
	if cityName == "" {
		cityName = c.Query("cityName")
	}

	var attractions []model.Attractions
	result := Util.DB().Where("attractions_name LIKE ?", "%"+cityName+"%").Find(&attractions)
	if result.Error != nil {
		c.String(http.StatusInternalServerError, result.Error.Error())
		return
	}

	c.HTML(http.StatusOK, "index.html", gin.H{"res": attractions})
}

func City_AttractionsHandler(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", "200")
}

func HandlerDaLi(c *gin.Context) {
	c.HTML(http.StatusOK, "dali.html", "200")
}
func HandlerAoMen(c *gin.Context) {
	c.HTML(http.StatusOK, "aomen.html", "200")
}
func HandlerWeiNiSi(c *gin.Context) {
	c.HTML(http.StatusOK, "weinisi.html", "200")
}

func Handlerpayment(c *gin.Context) {
	var But []model.But
	Util.DB().Table("payment").Find(&But)
	c.HTML(http.StatusOK, "buy.html", gin.H{"code": 200, "ccd": But})

}
